package visao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;

import javax.swing.JFormattedTextField;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JRadioButton;
import javax.swing.JButton;

import controle.ControleCliente;

import modelo.Cliente;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class CadastraCliente extends JFrame {

	private JPanel contentPane;
	private JTextField txtNome;
	private JTextField txtLogin;
	private JTextField txtSenha;
	private JTextField txtCpf;
	private JTextField txtTelefone;
	private JTextField txtEndereco;
	private JTextField txtComplemento;
	private JTextField txtBairro;
	private JTextField txtCep;
	private JTextField txtData;
	private JRadioButton rdbtnMasculino;
	private JRadioButton rdbtnFeminino;
	private JButton btnSalvar;
	private JButton btnCancelar;
	
	private String sexo;
	
	private Cliente cliente = new Cliente();

	private ControleCliente controleCliente;	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CadastraCliente frame = new CadastraCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CadastraCliente() {
		controleCliente = new ControleCliente();
		setTitle("CADASTRA CLIENTE");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 473, 517);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome Completo:");
		lblNome.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblNome.setForeground(Color.WHITE);
		lblNome.setBounds(10, 163, 126, 14);
		contentPane.add(lblNome);
		
		txtNome = new JTextField();
		txtNome.setForeground(Color.WHITE);
		txtNome.setBackground(new Color(0, 0, 0));
		txtNome.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtNome.setBounds(10, 177, 190, 20);
		contentPane.add(txtNome);
		txtNome.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Login:");
		lblNewLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setBounds(10, 57, 46, 14);
		contentPane.add(lblNewLabel);
		
		txtLogin = new JTextField();
		txtLogin.setForeground(Color.WHITE);
		txtLogin.setBackground(new Color(0, 0, 0));
		txtLogin.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtLogin.setBounds(10, 71, 139, 20);
		contentPane.add(txtLogin);
		txtLogin.setColumns(10);
		
		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblSenha.setForeground(Color.WHITE);
		lblSenha.setBounds(180, 57, 46, 14);
		contentPane.add(lblSenha);
		
		txtSenha = new JTextField();
		txtSenha.setForeground(Color.WHITE);
		txtSenha.setBackground(new Color(0, 0, 0));
		txtSenha.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtSenha.setBounds(179, 71, 139, 20);
		contentPane.add(txtSenha);
		txtSenha.setColumns(10);
		
		JLabel lblDadosDe = new JLabel("Dados de Login");
		lblDadosDe.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		lblDadosDe.setForeground(new Color(51, 102, 153));
		lblDadosDe.setBounds(10, 11, 216, 20);
		contentPane.add(lblDadosDe);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 44, 445, 2);
		contentPane.add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 119, 445, 2);
		contentPane.add(separator_1);
		
		JLabel lblDadosPessoais = new JLabel("Dados Pessoais");
		lblDadosPessoais.setForeground(new Color(51, 102, 153));
		lblDadosPessoais.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		lblDadosPessoais.setBounds(10, 132, 216, 20);
		contentPane.add(lblDadosPessoais);
		
		JLabel lblDataNascimento = new JLabel("Data Nascimento:");
		lblDataNascimento.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblDataNascimento.setForeground(Color.WHITE);
		lblDataNascimento.setBounds(257, 163, 126, 14);
		contentPane.add(lblDataNascimento);
		
		JLabel lblCpf = new JLabel("CPF:");
		lblCpf.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblCpf.setForeground(Color.WHITE);
		lblCpf.setBounds(10, 208, 126, 14);
		contentPane.add(lblCpf);
		
		try {
			txtCpf = new JFormattedTextField(new MaskFormatter(
					"###.###.###-##"));
			txtCpf.setForeground(Color.WHITE);
			txtCpf.setBackground(new Color(0, 0, 0));
			txtCpf.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		txtCpf.setColumns(10);
		txtCpf.setBounds(10, 222, 110, 20);
		contentPane.add(txtCpf);
		
		try {
			txtTelefone = new JFormattedTextField(new MaskFormatter(
					"(##)####-####"));
			txtTelefone.setForeground(Color.WHITE);
			txtTelefone.setBackground(new Color(0, 0, 0));
			txtTelefone.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		txtTelefone.setColumns(10);
		txtTelefone.setBounds(163, 222, 110, 20);
		contentPane.add(txtTelefone);
		
		JLabel lblTelefone = new JLabel("Telefone:");
		lblTelefone.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblTelefone.setForeground(Color.WHITE);
		lblTelefone.setBounds(163, 208, 110, 14);
		contentPane.add(lblTelefone);
		
		JLabel lblSexo = new JLabel("Sexo:");
		lblSexo.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblSexo.setForeground(Color.WHITE);
		lblSexo.setBounds(318, 208, 110, 14);
		contentPane.add(lblSexo);
		
		rdbtnMasculino = new JRadioButton("Masculino");
		rdbtnMasculino.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		rdbtnMasculino.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (rdbtnMasculino.isSelected()) {
					rdbtnFeminino.setSelected(false);
					rdbtnMasculino.setSelected(true);

					sexo = "M";
				}
				
			}
		});
		rdbtnMasculino.setForeground(Color.WHITE);
		rdbtnMasculino.setBackground(Color.BLACK);
		rdbtnMasculino.setBounds(318, 221, 109, 23);
		contentPane.add(rdbtnMasculino);
		
		rdbtnFeminino = new JRadioButton("Feminino");
		rdbtnFeminino.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		rdbtnFeminino.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if (rdbtnFeminino.isSelected()) {
					rdbtnMasculino.setSelected(false);
					rdbtnFeminino.setSelected(true);

					sexo = "F";
				}
				
			}
		});
		rdbtnFeminino.setForeground(Color.WHITE);
		rdbtnFeminino.setBackground(Color.BLACK);
		rdbtnFeminino.setBounds(319, 247, 109, 23);
		contentPane.add(rdbtnFeminino);
		
		txtEndereco = new JTextField();
		txtEndereco.setForeground(Color.WHITE);
		txtEndereco.setBackground(new Color(0, 0, 0));
		txtEndereco.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtEndereco.setColumns(10);
		txtEndereco.setBounds(10, 335, 190, 20);
		contentPane.add(txtEndereco);
		
		JLabel lblEndereo = new JLabel("Endere\u00E7o:");
		lblEndereo.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblEndereo.setForeground(Color.WHITE);
		lblEndereo.setBounds(10, 321, 126, 14);
		contentPane.add(lblEndereo);
		
		txtComplemento = new JTextField();
		txtComplemento.setForeground(Color.WHITE);
		txtComplemento.setBackground(new Color(0, 0, 0));
		txtComplemento.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtComplemento.setColumns(10);
		txtComplemento.setBounds(236, 335, 102, 20);
		contentPane.add(txtComplemento);
		
		JLabel lblComplemento = new JLabel("Complemento:");
		lblComplemento.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblComplemento.setForeground(Color.WHITE);
		lblComplemento.setBounds(236, 321, 126, 14);
		contentPane.add(lblComplemento);
		
		txtBairro = new JTextField();
		txtBairro.setForeground(Color.WHITE);
		txtBairro.setBackground(new Color(0, 0, 0));
		txtBairro.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		txtBairro.setColumns(10);
		txtBairro.setBounds(10, 380, 164, 20);
		contentPane.add(txtBairro);
		
		JLabel lblBairro = new JLabel("Bairro:");
		lblBairro.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblBairro.setForeground(Color.WHITE);
		lblBairro.setBounds(10, 366, 126, 14);
		contentPane.add(lblBairro);
		
		JLabel lblCep = new JLabel("CEP:");
		lblCep.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		lblCep.setForeground(Color.WHITE);
		lblCep.setBounds(234, 366, 126, 14);
		contentPane.add(lblCep);
		
		try {
			txtCep = new JFormattedTextField(new MaskFormatter(
					"#####-###"));
			txtCep.setForeground(Color.WHITE);
			txtCep.setBackground(new Color(0, 0, 0));
			txtCep.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		txtCep.setColumns(10);
		txtCep.setBounds(234, 380, 164, 20);
		contentPane.add(txtCep);
		
		try {
			txtData = new JFormattedTextField(new MaskFormatter(
					"##/##/####"));
			txtData.setForeground(Color.WHITE);
			txtData.setBackground(new Color(0, 0, 0));
			txtData.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		txtData.setColumns(10);
		txtData.setBounds(257, 177, 110, 20);
		contentPane.add(txtData);
		
		btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				SimpleDateFormat sdf = new SimpleDateFormat ("dd/MM/yyyy");
				
				cliente.setNome(txtNome.getText());
				cliente.setLogin(txtLogin.getText());
				cliente.setSenha(txtSenha.getText());
				cliente.setCpf(txtCpf.getText());
				cliente.setTelefone(txtTelefone.getText());
				cliente.setEndereco(txtEndereco.getText());
				cliente.setComplemento(txtComplemento.getText());
				cliente.setBairro(txtBairro.getText());
				cliente.setCep(txtCep.getText());
				cliente.setSexo(sexo);
				try {
					cliente.setDataNascimento(sdf.parse(txtData.getText()));
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}	
				
				
				
				
				
				
				try {
					controleCliente.salvar(cliente);
					JOptionPane.showMessageDialog(null, "Cliente salvo com sucesso!");
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, ("Erro ao salvar"));
					ex.printStackTrace();
				}
			
				txtNome.setText(null);
				txtLogin.setText(null);
				txtSenha.setText(null);
				txtCpf.setText(null);
				txtTelefone.setText(null);
				txtEndereco.setText(null);
				txtComplemento.setText(null);
				txtBairro.setText(null);
				txtCep.setText(null);
				txtData.setText(null);
				
				rdbtnFeminino.setSelected(false);
				rdbtnMasculino.setSelected(false);				
				
				dispose();
				
			}
		});
		btnSalvar.setForeground(Color.WHITE);
		btnSalvar.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		btnSalvar.setBackground(new Color(51, 102, 153));
		btnSalvar.setBounds(10, 456, 168, 23);
		contentPane.add(btnSalvar);
		
		btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				txtNome.setText(null);
				txtLogin.setText(null);
				txtSenha.setText(null);
				txtCpf.setText(null);
				txtTelefone.setText(null);
				txtEndereco.setText(null);
				txtComplemento.setText(null);
				txtBairro.setText(null);
				txtCep.setText(null);
				txtData.setText(null);
				
				rdbtnFeminino.setSelected(false);
				rdbtnMasculino.setSelected(false);				
				
				dispose();
				
			
				
			}
		});
		btnCancelar.setForeground(Color.WHITE);
		btnCancelar.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		btnCancelar.setBackground(new Color(51, 102, 153));
		btnCancelar.setBounds(236, 456, 168, 23);
		contentPane.add(btnCancelar);
		
		JLabel lblEndereo_1 = new JLabel("Endere\u00E7o");
		lblEndereo_1.setForeground(new Color(51, 102, 153));
		lblEndereo_1.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		lblEndereo_1.setBounds(10, 290, 216, 20);
		contentPane.add(lblEndereo_1);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(10, 277, 445, 2);
		contentPane.add(separator_2);
		
		JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(10, 427, 445, 2);
		contentPane.add(separator_3);
	}
}
